package cn.waka.sys.admin.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import cn.waka.sys.admin.service.ChannelService;
import cn.waka.sys.admin.service.IdcService;
import cn.waka.sys.admin.utils.DateUtils;

@RestController
@RequestMapping("/idc")
public class IdcController extends BaseController {
    @Autowired
    private ChannelService channelService;

    @Autowired
    private IdcService idcService;

    @GetMapping(path = "/user_live_daily", name = "活跃用户统计页")
    public ModelAndView userLiveDaily(HttpServletRequest req, HttpServletResponse res) {

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar1 = Calendar.getInstance();
        calendar1.add(Calendar.DATE, -7); //得到前7天
        String start_date = df.format(calendar1.getTime());

        Calendar calendar2 = Calendar.getInstance();
        calendar2.add(Calendar.DATE, -1); //得到前7天
        String end_date = df.format(calendar2.getTime());

        //渠道
        List<Map<String, Object>> channelList = channelService.getChannelList();

        ModelAndView mv = new ModelAndView();
        mv.addObject("start_date", start_date);
        mv.addObject("end_date", end_date);
        mv.addObject("channelList", channelList);
        return mv;

    }

    @RequestMapping(path = "/user_live_daily_data", produces = "application/json; charset=UTF-8", name = "活跃用户统计页数据")
    public Map<String, Object> user_live_daily_data(HttpServletRequest req, HttpServletResponse res) {
        String start_date = req.getParameter("start_date");
        String end_date = req.getParameter("end_date");
        String type = req.getParameter("type");
        String channel = req.getParameter("channel");
        List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
        if ("day".equals(type)) {
            dataList = idcService.getUserLive(start_date, end_date, channel);
        }

        List<String> dates = DateUtils.collectLocalDates(start_date, end_date);


        List<String> x_data = new ArrayList<String>();
        List<Integer> y_data = new ArrayList<Integer>();
        if ("day".equals(type)) {
            x_data = dates;
            for (String date : x_data) {
                int count = 0;
                for (Map<String, Object> data : dataList) {
                    if (date.equals(data.get("c_date"))) {
                        count = (int) data.get("count");
                    }
                }
                y_data.add(count);
            }
        }

        Map<String, Object> rs = new HashMap<String, Object>();
        rs.put("code", 0);
        rs.put("x_data", x_data);
        rs.put("y_data", y_data);
        return rs;
    }

    @GetMapping(path = "/user_new_daily", name = "新增用户统计页")
    public ModelAndView userNewDaily(HttpServletRequest req, HttpServletResponse res) {

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar1 = Calendar.getInstance();
        calendar1.add(Calendar.DATE, -7); //得到前7天
        String start_date = df.format(calendar1.getTime());

        Calendar calendar2 = Calendar.getInstance();
        calendar2.add(Calendar.DATE, -1); //得到前7天
        String end_date = df.format(calendar2.getTime());

        //渠道
        List<Map<String, Object>> channelList = channelService.getChannelList();

        ModelAndView mv = new ModelAndView();
        mv.addObject("start_date", start_date);
        mv.addObject("end_date", end_date);
        mv.addObject("channelList", channelList);
        return mv;

    }

    @RequestMapping(path = "/user_new_daily_data", produces = "application/json; charset=UTF-8", name = "新增用户统计页数据")
    public Map<String, Object> user_new_daily_data(HttpServletRequest req, HttpServletResponse res) {
        String start_date = req.getParameter("start_date");
        String end_date = req.getParameter("end_date");
        String type = req.getParameter("type");
        String channel = req.getParameter("channel");
        List<Map<String, Object>> dataList = idcService.getUserNew(start_date, end_date, channel, type);

        List<String> dates = DateUtils.collectLocalDates(start_date, end_date);


        List<String> x_data = new ArrayList<String>();
        List<Integer> y_data = new ArrayList<Integer>();
        if ("day".equals(type)) {
            x_data = dates;
            for (String date : x_data) {
                int count = 0;
                for (Map<String, Object> data : dataList) {
                    if (date.equals(data.get("c_date"))) {
                        count = (int) data.get("count");
                    }
                }
                y_data.add(count);
            }
        } else if ("hour".equals(type)) {
            for (String date : dates) {
                for (int h = 0; h < 24; h++) {
                    x_data.add(date + " " + h + "点");
                    int count = 0;
                    for (Map<String, Object> data : dataList) {
                        if (date.equals(data.get("c_date")) && h == (int) data.get("c_hour")) {
                            count = (int) data.get("count");
                        }
                    }
                    y_data.add(count);
                }
            }
        }

        Map<String, Object> rs = new HashMap<String, Object>();
        rs.put("code", 0);
        rs.put("x_data", x_data);
        rs.put("y_data", y_data);
        return rs;
    }

    @GetMapping(path = "/box_new_daily", name = "新增盒子统计页")
    public ModelAndView boxNewDaily(HttpServletRequest req, HttpServletResponse res) {

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar1 = Calendar.getInstance();
        calendar1.add(Calendar.DATE, -7); //得到前7天
        String start_date = df.format(calendar1.getTime());

        Calendar calendar2 = Calendar.getInstance();
        calendar2.add(Calendar.DATE, -1); //得到前7天
        String end_date = df.format(calendar2.getTime());

        //渠道
        List<Map<String, Object>> channelList = channelService.getChannelList();

        ModelAndView mv = new ModelAndView();
        mv.addObject("start_date", start_date);
        mv.addObject("end_date", end_date);
        mv.addObject("channelList", channelList);
        return mv;

    }

    @RequestMapping(path = "/box_new_daily_data", produces = "application/json; charset=UTF-8", name = "新增盒子统计页数据")
    public Map<String, Object> box_new_daily_data(HttpServletRequest req, HttpServletResponse res) {
        String start_date = req.getParameter("start_date");
        String end_date = req.getParameter("end_date");
        String type = req.getParameter("type");
        String channel = req.getParameter("channel");
        List<Map<String, Object>> dataList = idcService.getBoxNew(start_date, end_date, channel, type);

        List<String> dates = DateUtils.collectLocalDates(start_date, end_date);


        List<String> x_data = new ArrayList<String>();
        List<Integer> y_data = new ArrayList<Integer>();
        if ("day".equals(type)) {
            x_data = dates;
            for (String date : x_data) {
                int count = 0;
                for (Map<String, Object> data : dataList) {
                    if (date.equals(data.get("c_date"))) {
                        count = (int) data.get("count");
                    }
                }
                y_data.add(count);
            }
        } else if ("hour".equals(type)) {
            for (String date : dates) {
                for (int h = 0; h < 24; h++) {
                    x_data.add(date + " " + h + "点");
                    int count = 0;
                    for (Map<String, Object> data : dataList) {
                        if (date.equals(data.get("c_date")) && h == (int) data.get("c_hour")) {
                            count = (int) data.get("count");
                        }
                    }
                    y_data.add(count);
                }
            }
        }

        Map<String, Object> rs = new HashMap<String, Object>();
        rs.put("code", 0);
        rs.put("x_data", x_data);
        rs.put("y_data", y_data);
        return rs;
    }

    @GetMapping(path = "/box_live_daily", name = "活跃盒子统计页")
    public ModelAndView boxLiveDaily(HttpServletRequest req, HttpServletResponse res) {

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar1 = Calendar.getInstance();
        calendar1.add(Calendar.DATE, -7); //得到前7天
        String start_date = df.format(calendar1.getTime());

        Calendar calendar2 = Calendar.getInstance();
        calendar2.add(Calendar.DATE, -1); //得到前7天
        String end_date = df.format(calendar2.getTime());

        //渠道
        List<Map<String, Object>> channelList = channelService.getChannelList();

        ModelAndView mv = new ModelAndView();
        mv.addObject("start_date", start_date);
        mv.addObject("end_date", end_date);
        mv.addObject("channelList", channelList);
        return mv;

    }

    @RequestMapping(path = "/box_live_daily_data", produces = "application/json; charset=UTF-8", name = "活跃盒子统计页数据")
    public Map<String, Object> box_live_daily_data(HttpServletRequest req, HttpServletResponse res) {
        String start_date = req.getParameter("start_date");
        String end_date = req.getParameter("end_date");
        String type = req.getParameter("type");
        String channel = req.getParameter("channel");
        List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
        if ("day".equals(type)) {
            dataList = idcService.getBoxLive(start_date, end_date, channel);
        }

        List<String> dates = DateUtils.collectLocalDates(start_date, end_date);


        List<String> x_data = new ArrayList<String>();
        List<Integer> y_data = new ArrayList<Integer>();
        if ("day".equals(type)) {
            x_data = dates;
            for (String date : x_data) {
                int count = 0;
                for (Map<String, Object> data : dataList) {
                    if (date.equals(data.get("c_date"))) {
                        count = (int) data.get("count");
                    }
                }
                y_data.add(count);
            }
        }

        Map<String, Object> rs = new HashMap<String, Object>();
        rs.put("code", 0);
        rs.put("x_data", x_data);
        rs.put("y_data", y_data);
        return rs;
    }
}
